package com.quanyan.stadium.biz.impl;

import com.quanyan.stadium.api.resp.RespUserCategoryVo;
import com.quanyan.stadium.biz.BizSportCardDao;
import com.quanyan.stadium.entity.TbGameCategoryUser;
import com.quanyan.stadium.entity.TbGameCategoryUserExample;
import com.quanyan.stadium.entity.vo.CategoryPriority;
import com.quanyan.stadium.enums.GameCategoryEnum;
import com.quanyan.stadium.mapper.TbGameCategoryUserMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongshenglin
 * @date 2016/7/18  15:55
 */
@Service
public class BizSportCardDaoImpl implements BizSportCardDao{

    @Autowired
    private TbGameCategoryUserMapper tbGameCategoryUserMapper;

    public List<RespUserCategoryVo> getUserCategoryList(Integer uid) {
        List<RespUserCategoryVo> respUserCategories = new ArrayList<>();
        if ( null != uid){
            TbGameCategoryUserExample example = new TbGameCategoryUserExample();
            TbGameCategoryUserExample.Criteria criteria = example.createCriteria();
            example.setOrderByClause("priority");
            criteria.andUidEqualTo(uid);
            List<TbGameCategoryUser> userList = tbGameCategoryUserMapper.selectByExample(example);
            if (null!=userList && userList.size()>0) {
                for (TbGameCategoryUser tbGameCategoryUser : userList) {
                    RespUserCategoryVo respUserCategoryVo = new RespUserCategoryVo();
                    try {
                        BeanUtils.copyProperties(respUserCategoryVo, tbGameCategoryUser);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    respUserCategoryVo.setName(GameCategoryEnum.getMsgById(tbGameCategoryUser.getGameCategoryId()).getName());
                    respUserCategories.add(respUserCategoryVo);
                }
            }
        }
        return respUserCategories;
    }

    @Override
    public void setCategoryPriority(CategoryPriority categoryPriority) {
        TbGameCategoryUserExample example = new TbGameCategoryUserExample();
        TbGameCategoryUserExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(categoryPriority.getUid());
        criteria.andGameCategoryIdEqualTo(categoryPriority.getGameCategoryId());


        TbGameCategoryUser tbGameCategoryUser = new TbGameCategoryUser();
        tbGameCategoryUser.setIsObtain(categoryPriority.getIsObtain());
        tbGameCategoryUser.setPriority(categoryPriority.getPriority());

        tbGameCategoryUserMapper.updateByExampleSelective(tbGameCategoryUser, example);
    }
}
